public without sharing class ClockInOutController {
    public ClockInOutHistory__c cioHistory {get;set;}
    /*
        0 --  按钮显示为上班打卡
        1 --- 则按钮显示下班打卡
        2 --- 上下班都已打卡，则按钮显示为灰色不可点
    */
    public Integer type {Get;Set;}//状态位

    public ClockInOutController(ApexPages.StandardController con) {}

    public void init(){
        List<ClockInOutHistory__c> clockInOut = [
            SELECT Id,ClockOutLocation__c,ClockInLocation__c,ClockInTime__c,ClockOutTime__c,User__c 
            FROM ClockInOutHistory__c 
            WHERE User__c =: UserInfo.getUserId() AND CreatedDate >= TODAY];
        if(null != clockInOut && clockInOut.size() > 0){
            cioHistory = clockInOut.get(0);
            if(cioHistory.ClockInTime__c == null && cioHistory.ClockOutTime__c == null){
                type = 0;
            }else if(cioHistory.ClockInTime__c != null && cioHistory.ClockOutTime__c == null){
                type = 1;
            }else{
                type = 2;
            }
        }else{
            cioHistory = new ClockInOutHistory__c(User__c = UserInfo.getUserId());
            Insert cioHistory;
        }
    }

    
    @RemoteAction
    public static String setLocation(Decimal lat,Decimal lng, String location, String type){
        String status = '';
        try{
            Datetime now = Datetime.now();
            // String staff_status = u.Status__c;
            User u = [select Id, Status__c from User where Id = :UserInfo.getUserId()];

            ClockInOutHistory__c cioHistory = [SELECT Id,ClockOutLocation__c,
                ClockInLocation__c,ClockInTime__c,ClockOutTime__c,User__c 
                FROM ClockInOutHistory__c 
                WHERE User__c =: UserInfo.getUserId() 
                AND CreatedDate >= TODAY LIMIT 1];

            if(type == '上班'){//记录上班打卡信息
                cioHistory.ClockInTime__c = now;
                cioHistory.ClockInLocation__Latitude__s = lat;
                cioHistory.ClockInLocation__Longitude__s = lng;
                cioHistory.InLocation__c = location;
                // 如果是在移动/工作中，保持原本状态
                if (u.Status__c != '工作中' && u.Status__c != '移动中') {
                    u.Status__c = '待命';
                }
            }else if(type == '下班'){//记录下班打卡信息
                cioHistory.ClockOutTime__c = now;
                cioHistory.ClockOutLocation__Latitude__s = lat;
                cioHistory.ClockOutLocation__Longitude__s = lng;
                cioHistory.OutLocation__c = location;
                // 如果是在移动/工作中，保持原本状态
                if (u.Status__c != '工作中' && u.Status__c != '移动中') {
                    u.Status__c = '离线';
                }
            }
            // 更新打卡记录
            update cioHistory;
            // 更新用户状态
            update u;
            
            if(type=='上班'){
                status = '上班打卡成功'; 
            }else if(type=='下班'){
                status = '下班打卡成功';
            }
        }catch(Exception ex){
            status ='打卡失败';
            Utils.insertLog('上下班打卡', UserInfo.getUserId(), ex.getMessage());
        }
        System.debug(LoggingLevel.INFO, '*** status: ' + status);
        return status;
    }

}